<?php
/**
 * User: onwer
 * Date: 11-11-10
 * Time: 下午10:16
 */

class Manage extends DB
{
    public function __construct()
    {
        parent::__construct('f_system_manage');
    }

    /**
     * @param  $catname  目录名称
     * @param int $cattplid 模板类型，表ID
     * @param string $listfile 列表文件
     * @param string $listtpl 列表模板
     * @param string $filetpl 内容模板
     * @param string $namerule 命名规则
     * @param int $pagenum 分页数目
     * @param string $filepath 文件路径
     * @param int $parent 父目录
     * @return   int 失败返回0，成功返回插入的ID
     */
    public function add($catname, $cattplid, $listfile = '', $listtpl = '', $filetpl = '', $namerule = '',
                        $pagenum = 20, $filepath = '', $parent = 0)
    {
        if (empty($catname)) {
            $this->errorinfo[2] = '目录名不能为空！';
            return false;
        }

        return $this->insert(array('cat_name' => $catname, 'cat_tpl_id' => $cattplid, 'list_flie' => $listfile,
            'list_tpl' => $listtpl, 'file_tpl' => $filetpl, 'name_rule' => $namerule,
            'page_num' => $pagenum, 'file_path' => $filepath, 'parent_catalog' => $parent));
    }

    /**
     * 更新目录
     * @param string $catname
     * @param int $cattplid
     * @param string $listfile
     * @param string $listtpl
     * @param string $filetpl
     * @param string $namerule
     * @param int $pagenum
     * @param string $filepath
     * @param int $mid manage表ID
     * @return bool
     */
    public function alt($catname, $cattplid, $listfile = '', $listtpl = '', $filetpl = '',
                        $namerule = '', $pagenum = 20, $filepath = '', $mid)
    {
        if (empty($catname)) {
            $this->errorinfo[2] = '目录名不能为空！';
            return false;
        }

        return $this->update(array('cat_name' => $catname, 'cat_tpl_id' => $cattplid, 'list_flie' => $listfile,
                'list_tpl' => $listtpl, 'file_tpl' => $filetpl, 'name_rule' => $namerule,
                'page_num' => $pagenum, 'file_path' => $filepath),
            sprintf('id=%d', $mid));
    }

    /**
     * 删除目录
     * @param  $id
     * @return bool
     */
    public function del($id)
    {
        //获取所有目录ID
        function getallid($mana, $id, $r, $tmpr = array())
        {
            array_push($r, $id);
            $tmpr = array_merge_recursive($tmpr, $mana->querypid($id));
            if (empty($tmpr)) {
                return $r;
            }
            $tmp = array_shift($tmpr);
            return getallid($mana, $tmp['id'], $r, $tmpr);

        }

        $allid = getallid($this, $id, array());
        return $this->delete(sprintf('id IN(%s)', implode(',', $allid)));

    }
    public function altacth($mid,$aid,$act=''){
        $manarr=$this->querymid($mid);
        if(empty($manarr)){
            $this->errorinfo[2]='没有这个表！';
            return false;
        }
        $aidarr=explode(',',$manarr[0]['attach_id']);
        if(!in_array($aid,$aidarr)&&$act===''){
            array_push($aidarr,$aid);
        }else if($act=='del'){
            array_splice($aidarr,array_search($aid,$aidarr),1);
        }


        return $this->update(array('attach_id'=>implode(',',$aidarr)),sprintf('id=%d',$mid));
    }
    public function altshowfiledid($mid,$fid,$act=''){
        $manarr=$this->querymid($mid);
        if(empty($manarr)){
            $this->errorinfo[2]='没有这个表！';
            return false;
        }
        $fidarr=explode(',',$manarr[0]['showfiledid']);
        if(!in_array($fid,$fidarr)&&$act===''){
            array_push($fidarr,$fid);
        }else if($act=='del'){
            array_splice($fidarr,array_search($fid,$fidarr),1);
        }

        return $this->update(array('showfiledid'=>implode(',',$fidarr)),sprintf('id=%d',$mid));
    }
    public function querymid($mid)
    {
        return $this->select(sprintf('id=%d', $mid));
    }

    public function querypid($pid)
    {
        return $this->select(sprintf('parent_catalog=%d', $pid));
    }

    public function querytable($mid)
    {
        $manarr = $this->querymid($mid);
        if(empty($manarr)){
            return array();
        }
        return $this->select(sprintf('id=%d', $manarr[0]['cat_tpl_id']), '', '*', 'f_system_table');
    }
    public function getallfiled($mid)
    {

        $curdirarr = $this->querymid($mid);
        $filed = new Filed();
        return $filed->querytid($curdirarr[0]['cat_tpl_id']);
    }
    /**
     * @param $mid
     * @param $id
     * @param string $limit
     * @param string $filedstr 字段  * 完全现实  limit 有限制的显示
     * @param string $isdesc 是否as描述
     * @return array
     */
    public function queryspec($mid, $id=-1,$limit = '',$filedstr='limit',$isdesc='as %s')
    {
        $tablearr = $this->querytable($mid);
        $condition = $id === -1 ? sprintf('cat_id=%d' , $mid) : sprintf('id=%d', $id);
        $filed = new Filed();
        $filearr = $filed->querytid($tablearr[0]['id']);
        $curdirarr=$this->querymid($mid);
        //所有的字段
        $showfiled = sprintf('id , sys_time '.$isdesc,'时间');
        if($filedstr==='*'){

            foreach ($filearr as $v) {
                $showfiled .= sprintf(',%s '.$isdesc, $v['filed_name'], $v['filed_desc']);
            }
        }else if($filedstr==='limit'){
            foreach ($filearr as $v) {
                if(in_array($v['id'],explode(',',$curdirarr[0]['showfiledid']))){
                    $showfiled .= sprintf(',%s '.$isdesc, $v['filed_name'], $v['filed_desc']);
                }
            }

        }else{
            $showfiled = $filedstr;
        }

        return $this->select($condition, $limit, $showfiled, $tablearr[0]['table_name']);
    }


}

